package com.meiyu.live.tv.widget;

import android.content.Context;
import android.text.Editable;
import android.text.Selection;
import android.text.TextWatcher;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.FrameLayout;

import com.meiyu.live.R;

/**
 * Created by zbl on 2016/9/6.
 * 输入文字和弹幕view
 */
public class InputView extends FrameLayout implements View.OnClickListener {

    private static final int MAX_DANMU_TEXT_LENGTH = 20;

    public interface OnInputListener {
        void onSend(String text, boolean isDanmu);
    }

    private OnInputListener onInputListener;

    private DanmuSwitch sw_danmu;
    private EditText et_input;
    private View btn_send;

    private boolean isDanmu;

    public InputView(Context context) {
        super(context);
        init(context);
    }

    public InputView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init(context);
    }

    private void init(Context context) {
        LayoutInflater.from(context).inflate(R.layout.view_input, this);
        sw_danmu = (DanmuSwitch) findViewById(R.id.sw_danmu);
        sw_danmu.setOnDanmuSwitchChangeListener(new DanmuSwitch.OnDanmuSwitchChangeListener() {
            @Override
            public void onCheckedChanged(boolean isChecked) {
                isDanmu = isChecked;
                if (isChecked && et_input.getText().length() > MAX_DANMU_TEXT_LENGTH) {
                    //截取新字符串
                    String newStr = et_input.getText().toString().substring(0, MAX_DANMU_TEXT_LENGTH);
                    et_input.setText(newStr);
                    Selection.setSelection(et_input.getText(), et_input.getText().length());
                }
            }
        });
        et_input = (EditText) findViewById(R.id.et_input);
        et_input.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (isDanmu) {
                    Editable editable = et_input.getText();
                    int len = editable.length();

                    if (len > MAX_DANMU_TEXT_LENGTH) {
                        int selEndIndex = Selection.getSelectionEnd(editable);
                        String str = editable.toString();
                        //截取新字符串
                        String newStr = str.substring(0, MAX_DANMU_TEXT_LENGTH);
                        et_input.setText(newStr);
                        editable = et_input.getText();

                        //新字符串的长度
                        int newLen = editable.length();
                        //旧光标位置超过字符串长度
                        if (selEndIndex > newLen) {
                            selEndIndex = editable.length();
                        }
                        //设置新光标所在的位置
                        Selection.setSelection(editable, selEndIndex);

                    }
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        btn_send = findViewById(R.id.btn_send);
        btn_send.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        if (view == btn_send) {
            if (onInputListener != null) {
                onInputListener.onSend(et_input.getText().toString().trim(), sw_danmu.getChecked());
            }
            et_input.setText("");
        }
    }

    public void setOnInputListener(OnInputListener onInputListener) {
        this.onInputListener = onInputListener;
    }

    public EditText getEditView() {
        return et_input;
    }
}
